package com.yuandengta.yomall.coupon.service.impl;

import com.yuandengta.common.to.SkuReductionTO;
import com.yuandengta.yomall.coupon.entity.MemberPriceEntity;
import com.yuandengta.yomall.coupon.entity.SkuLadderEntity;
import com.yuandengta.yomall.coupon.service.MemberPriceService;
import com.yuandengta.yomall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandengta.common.utils.PageUtils;
import com.yuandengta.common.utils.Query;

import com.yuandengta.yomall.coupon.dao.SkuFullReductionDao;
import com.yuandengta.yomall.coupon.entity.SkuFullReductionEntity;
import com.yuandengta.yomall.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存满减打折及会员价
     * @param skuReductionTO
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveSkuReduction(SkuReductionTO skuReductionTO) {
        // 满几件减 sms_sku_ladder
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(skuReductionTO, skuLadderEntity);
        skuLadderEntity.setAddOther(skuReductionTO.getCountStatus());
        if (skuLadderEntity.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);
        }

        // 满多少价减 sms_sku_full_reduction
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTO, skuFullReductionEntity);
        skuFullReductionEntity.setAddOther(skuReductionTO.getPriceStatus());
        if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
            this.save(skuFullReductionEntity);
        }

        // 会员价 sms_member_price
        List<MemberPriceEntity> memberPriceEntities = skuReductionTO.getMemberPrice().stream()
                .map(memberPrice -> {
                    MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                    memberPriceEntity.setSkuId(skuReductionTO.getSkuId());
                    memberPriceEntity.setMemberLevelId(memberPrice.getId());
                    memberPriceEntity.setMemberLevelName(memberPrice.getName());
                    memberPriceEntity.setMemberPrice(memberPrice.getPrice());
                    memberPriceEntity.setAddOther(skuReductionTO.getPriceStatus());
                    return memberPriceEntity;
                }).filter(memberPriceEntity -> {
                    return memberPriceEntity.getMemberPrice().compareTo(new BigDecimal("0")) == 1;
                }).collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntities);
    }

}